#ifndef COMMON_H
#define COMMON_H

#include <iostream>
#include <string>
#include <cstring> // For memcpy

constexpr int LENGTH_PREFIX_SIZE = sizeof(uint32_t);    // 4 bytes for length

// Platform-specific headers for networking
#ifdef _WIN32
    #include <winsock2.h>
    #pragma comment(lib, "ws2_32.lib")
#else
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <unistd.h>
#endif

// Helper function for platform independence
inline void init_sockets() {
#ifdef _WIN32
    WSADATA wsaData;
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        std::cerr << "WSAStartup failed.\n";
    }
#endif
}

inline void cleanup_sockets() {
#ifdef _WIN32
    WSACleanup();
#endif
}

inline void close_socket(int fd) {
#ifdef _WIN32
    closesocket(fd);
#else
    close(fd);
#endif
}

// Convert host long to network long (for length prefix)
inline uint32_t host_to_network(uint32_t val) {
    return htonl(val);
}

// Convert network long to host long (for length prefix)
inline uint32_t network_to_host(uint32_t val) {
    return ntohl(val);
}

#endif // COMMON_H